'use strict';
app.controller('&LFobjectname&ItemController', function($controller, $scope, $rootScope, $http,$filter, pageService,  BaseService) {
	$scope.defaultItemInfo = function() {
		var tempId = BaseService.getId();
		return {
			id: tempId,
			dataOperationType: 'new',
			createdBy: $rootScope.user.id,
            orgId: $rootScope.user.org_id,
            postnId: $rootScope.user.postn_id
            };
        }
	$scope.currentDataVar = "&LFobjectname&Item";
	angular.extend(this, $controller('BasicItemController', {$scope: $scope}));
    $scope.modelType = '&LFobjectname&';
    $scope.modelObj = $scope.&LFobjectname&Item;
    //新建或更新
    $scope.insertOrUpdate&LFobjectname& = function () {
        if (!checkData()) {
            return;
        }
        $rootScope.showWaitModal();
        $http.post(ROOT.__URL__ + '/mobile/&LFobjectname&/upsert',
            $scope.&LFobjectname&Item
        ).success(function (data) {
            $rootScope.hideWaitModal();
            if (data.success) {
                if (isEmpty($scope.&LFobjectname&Item.id)) {
                    if (isEmpty($scope.otherModelTo&LFobjectname&Flag) && typeof($scope.init&LFobjectname&) != "undefined") {
                        $scope.&LFobjectname&Item = data.result;
                        $scope.init&LFobjectname&($scope.&LFobjectname&Status);
                        &LFobjectname&Navigator.popPage();
                    } else {
                        $rootScope.gotoMenu("&LFobjectname&");
                    }
                } else if ($scope.otherModelTo&LFobjectname&Flag) {
                    $rootScope.gotoMenu("&LFobjectname&");
                } else {
                    &LFobjectname&Navigator.popPage();
                }
                showAlert("保存成功!");
            } else {
                showAlert("保存失败:" + data.message);
            }
        })
            .error(function (data) {
                $rootScope.hideWaitModal();
                showAlert("系统错误！");
            });
    };
    function checkData() {
        if (isEmpty($scope.&LFobjectname&Item.id)) {
            showAlert("ID必输!");
            return false;
        }
       
        return true;
    }
  //删除
	$scope.delete&LFobjectname& = function() {
		$rootScope.showWaitModal();
		$http.get(ROOT.__URL__ + '/mobile/&LFobjectname&/deleteById',
			{
				cache: false,
				params: {
					id: $scope.accntItem.id
				}
			}
		).success(function(data) {
				$rootScope.hideWaitModal();
				if (data.success) {
					if ($scope.otherModelToAccntFlag) {
						$rootScope.gotoMenu("&LFobjectname&");
					} else {
						$scope.accntPageInfo = pageService.spliceOne($scope.accntPageInfo, $scope.accntItem.id);
						acntNavigator.popPage();
					}
					showAlert("已删除");
				} else {
					showAlert("删除失败：" + data.message);
				}
			})
			.error(function(data) {
				$rootScope.hideWaitModal();
				showAlert("系统错误!");
			});
	}

});
